package han.chensing.latos.game.elements.stage;

import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Disposable;
import han.chensing.latos.game.elements.LatosElement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LatosStage implements Disposable {
    private final List<LatosElement> elements;
    private final Batch batch;

    public LatosStage(){
        this.elements= Collections.synchronizedList(new ArrayList<>());
        this.batch=new SpriteBatch();
    }

    public List<LatosElement> getElements() {
        return elements;
    }

    public void act(float deltaTime,float currentTime){
        for (int i=0;i<elements.size();i++){
            LatosElement element = elements.get(i);
            element.lAct(deltaTime, currentTime);
            if (element.isDied())
                elements.remove(element);
        }
    }

    public void draw(){
        batch.begin();
        for (int i=0;i<elements.size();i++){
            LatosElement element = elements.get(i);
            if (element==null)
                continue;
            element.draw(batch,1.0f);
        }
        batch.end();
    }

    @Override
    public void dispose(){
        batch.dispose();
    }

    public void addElement(LatosElement element){
        this.elements.add(element);
    }

    public void combineCamera(Camera camera){
        batch.setProjectionMatrix(camera.combined);
    }
}
